package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.*;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.service.*;
import net.sf.jsqlparser.util.validation.metadata.NamedObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@Service
public class indexServiceImpl implements indexService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysRoleMenuService roleMenuService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysDictService dictService;

    @Autowired
    private IYddCateService cateService;

    @Autowired
    private IYddArticleService articleService;

    @Autowired
    private IYddAdvimgService advimgService;

    @Autowired
    private IYddAdvposService advposService;

    @Autowired
    private IYddTagService tagService;

    @Autowired
    private IYddUserService yddUserService;

    @Override
    public Map<String, Object> getProfile(String userId) {

        Map map = new HashMap<>();

        //通过id获得用户对象
        SysUser user = userService.getById(userId);

        UserInfo userInfo = new UserInfo();

        //数据拷贝
        BeanUtils.copyProperties(user,userInfo);

        List<String> roleIds = user.getRoleId();
        List<String> roleNameList = new ArrayList<>();

        for (String roleId : roleIds) {

            LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();

            roleLambdaQueryWrapper.eq(SysRole::getId,roleId);
            List<SysRole> sysRoleList = roleService.list(roleLambdaQueryWrapper);



            if (sysRoleList!=null){
                for (SysRole sysRole : sysRoleList) {
                    roleNameList.add(sysRole.getName());
                }
            }
        }



        userInfo.setRoleNameList(roleNameList);

        map.put("userInfo",userInfo);

        //获取系统菜单数据
        List<String>  menuIds= new ArrayList<>();
        for (String roleId : roleIds) {
            LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,roleId);

            List<SysRoleMenu> sysRoleMenus = roleMenuService.list(sysRoleMenuLambdaQueryWrapper);


            for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
                menuIds.add(sysRoleMenu.getMenuId());
            }
        }

        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMenuLambdaQueryWrapper.eq(SysMenu::getIsShow,1);
        sysMenuLambdaQueryWrapper.eq(SysMenu::getType,1);
        sysMenuLambdaQueryWrapper.in(SysMenu::getId,menuIds);
        List<SysMenu> menuList = menuService.list(sysMenuLambdaQueryWrapper);
        map.put("menuList",menuList);


        //字典表

        Map dictNap = new HashMap<>();

        List<SysDict> list = dictService.list();

        Map table_type = new HashMap<>();
        Map interface_type = new HashMap<>();
        Map report_type = new HashMap<>();
        Map theme = new HashMap<>();
        Map order_type = new HashMap();

        for (SysDict sysDict : list) {
            String type = sysDict.getType();
            String value = sysDict.getValue();
            String label = sysDict.getLabel();

            if (type.equals("interface_type")) {
                interface_type.put(value, label);
            } else if (type.equals("order_type")) {
                order_type.put(value, label);
            } else if (type.equals("report_type")) {
                report_type.put(value, label);
            } else if (type.equals("table_type")) {
                table_type.put(value, label);
            } else if (type.equals("theme")) {
                theme.put(value, label);
            }
        }
        dictNap.put("interface_type",interface_type);
        dictNap.put("table_type",table_type);
        dictNap.put("report_type",report_type);
        dictNap.put("theme",theme);
        dictNap.put("order_type",order_type);

        map.put("dictList",dictNap);
        map.put("btnList",new String[]{"*"});

        return map;



        }

    @Override
    public BaseInfoDto baseInfo() {

        BaseInfoDto baseInfoDto = new BaseInfoDto();

        List<YddCate> cates = cateService.list();

      List<cateDto>cateDtos= cates.stream().map((cate)->{
            cateDto cateDto = new cateDto();
            BeanUtils.copyProperties(cate,cateDto);
            LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            articleLambdaQueryWrapper.eq(YddArticle::getCateid,cate.getId());
            int count = articleService.count(articleLambdaQueryWrapper);
            cateDto.setNum(count);
            return cateDto;
        }).collect(Collectors.toList());

  baseInfoDto.setAllCateList(cateDtos);

       //设置管理员数量
        baseInfoDto.setAdminNum(userService.count());
        //设置图片数量
        baseInfoDto.setAdvingNum(advimgService.count());
        //设置广告位数量
        baseInfoDto.setAdvposNum(advposService.count());
        //文章数量
        baseInfoDto.setArticleNum(articleService.count());
        //广告分类数
        baseInfoDto.setCateNum(cateService.count());
        //标签总数
        baseInfoDto.setTagNum(tagService.count());

        //最新用户数量
        LambdaQueryWrapper<YddUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.select(YddUser::getId,YddUser::getUsername,YddUser::getSex,YddUser::getIcon,YddUser::getAddress);
        userLambdaQueryWrapper.orderByDesc(YddUser::getId);
        userLambdaQueryWrapper.last("limit 7");
        List<YddUser> yddUsers = yddUserService.list(userLambdaQueryWrapper);
        baseInfoDto.setNewUserList(yddUsers);

        //热门文章
        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.select(YddArticle::getId,YddArticle::getTitle,YddArticle::getAuthor,YddArticle::getKeywords,YddArticle::getClick);
        articleLambdaQueryWrapper.last("limit 7");
        List<YddArticle> articles = articleService.list(articleLambdaQueryWrapper);
        baseInfoDto.setArticleList(articles);

        //用户数
        UserDto userDto = new UserDto();
        LambdaQueryWrapper<YddUser> yddUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddUserLambdaQueryWrapper.eq(YddUser::getSex,0);
        //总数
        int count = yddUserService.count();

        //男性用户
        int manCount = yddUserService.count(yddUserLambdaQueryWrapper);
        //女性用户
        int womanCount = count - manCount;
        userDto.setTotal(count);

        userDto.setUserMan(manCount);

        userDto.setUserWoman(womanCount);
        baseInfoDto.setUser(userDto);

        return baseInfoDto;

    }


}
